﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WSAP;

namespace CouplingBeamProject
{
    /// <summary>
    /// 组件信息
    /// </summary>
    class Block: Rectangular
    {
        /// <summary>
        /// 块体编号
        /// </summary>
        private int BlockIndex { set; get; }

        /// <summary>
        /// 单元列表
        /// </summary>
        private List<Element> ElementList { set; get; }

        /// <summary>
        /// 厚度
        /// </summary>
        private double ThickFactor { set; get; }

        /// <summary>
        /// 添加单元
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool AddElements(WModelNet model)
        {
            foreach(var element in this.ElementList)
            {
                if (!element.AddElement(model,this.ThickFactor))
                    return false;
            }
            return true;
        }

        /// <summary>
        /// 获得单个块体所有单元的节点
        /// </summary>
        public List<Node> GetNodeList()
        {
            var nodeList = new List<Node>();
            this.ElementList.ForEach(element => nodeList.AddRange(element.GetNodeList()));
            return nodeList;
        }
        /// <summary>
        /// 重写单元列表
        /// </summary>
        /// <param name="EleList"></param>
        /// <returns></returns>
        public void Mesh()
        {
            //划分尺寸
            var scale = CouplingBeamProject.GlobalVar.MeshDimension;
            this.ElementList = new List<Element>();
            //判断尺寸是否为零
            if (this.Width == 0 || this.Height == 0) return;
            //横向纵向划分数目
            int NumX = (int)(this.Width / scale);
            int NumY = (int)(this.Height / scale);
            if (NumX != 0 && NumY != 0)
            {
                for (int j = 0; j < NumY; j++)
                {
                    for (int i = 0; i < NumX; i++)
                    {
                        var ww = i == NumX - 1 ? Width - i * scale : scale;
                        var hh = j == NumY - 1 ? Height - j * scale : scale;
                        var ix = this.LeftDownNode.X + scale * i;
                        var iy = this.LeftDownNode.Y + scale * j;
                        ElementList.Add(new Element(ww, hh, ix, iy, this.Material));
                    }
                }
            }
            else if (NumX == 0 && NumY != 0)
            {
                for (int j = 0; j < NumY; j++)
                {
                    var ww = Width;
                    var hh = j == NumY - 1 ? Height - j * scale : scale;
                    var ix = this.LeftDownNode.X;
                    var iy = this.LeftDownNode.Y + scale * j;
                    ElementList.Add(new Element(ww, hh, ix, iy, this.Material));
                }
            }
            else if (NumX != 0 && NumY == 0)
            {
                for (int i = 0; i < NumX; i++)
                {
                    var ww = i == NumX - 1 ? Width - i * scale : scale;
                    var hh = Height;
                    var ix = this.LeftDownNode.X + scale * i;
                    var iy = this.LeftDownNode.Y;
                    ElementList.Add(new Element(ww, hh, ix, iy, this.Material));
                }
            }
            else
            {
                ElementList.Add(new Element(Width, Height, this.LeftDownNode.X, this.LeftDownNode.Y, this.Material));
            }
        }

        ///// <summary>
        ///// 构造函数
        ///// </summary>
        //public Block(int index, double W, double H, double NodeX, double NodeY, Material material)
        //    : base(W, H, NodeX, NodeY, material)
        //{
        //    this.BlockIndex = index;
        //    this.Material = material;
        //    this.ThickFactor = 1;
        //}

        /// <summary>
        /// 构造函数
        /// </summary>
        public Block(int index, double W, double H, double NodeX, double NodeY, Material material, double thickfactor = 1)
            : base(W, H, NodeX, NodeY, material)
        {
            this.BlockIndex = index;
            this.Material = material;
            this.ThickFactor = thickfactor;
        }

    }
}
